import csv
import os
import time

import requests
from lxml import etree


def get_html(url):
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
        # 'Cookie':'__mta=108835517.1730957808851.1730965032971.1730965809578.264; uuid_n_v=v1; uuid=46B098409CCA11EF8D500DF0F6434901D95A4E5C01524DADBF1A190637686DF8; _csrf=b0740724db5d9ba460b824c75312953eec4320360ec9735cf172491fb2a1d0df; _lxsdk_cuid=192f5e55f9ac8-09250105a6bb97-26011951-144000-192f5e55f9ac8; _lxsdk=46B098409CCA11EF8D500DF0F6434901D95A4E5C01524DADBF1A190637686DF8; Hm_lvt_e0bacf12e04a7bd88ddbd9c74ef2b533=1730702303,1730742403,1730744647,1730957806; HMACCOUNT=D5560BB41C487C12; _ga=GA1.1.742828068.1730957807; _ga_WN80P4PSY7=GS1.1.1730957807.1.1.1730966001.0.0.0; Hm_lpvt_e0bacf12e04a7bd88ddbd9c74ef2b533=1730966001; __mta=108835517.1730957808851.1730965809578.1730966001996.265; _lxsdk_s=19305200a44-546-0b1-61b%7C%7C545'
    }
    resp = requests.get(url,headers=headers)
    resp.encoding = 'utf-8'
    # print(resp.text)
    return resp.text

def parse_list(html):
    e = etree.HTML(html)
    # new_url =
    # https: // www.maoyan.com / films / 13645
    list_url = ['https://www.maoyan.com' + url for url in e.xpath('//div[@class="movie-item film-channel"]/a/@href')]
    print(list_url)
    return list_url

def main():
    for page in range(11):
        time.sleep(3)
        url = 'https://www.maoyan.com/films?showType=3&offset={}'.format(page * 30)
        print(url)
        list_html = get_html(url)
        list_url = parse_list(list_html)
        print(list_url)
        for murl in list_url:
            print(murl)
            save_to_csv([murl])
            # break
        # break


def init():
    if not os.path.exists('./movieUrl.csv'):
        with open('./movieUrl.csv','a',newline='',encoding='utf-8') as wf:
            write = csv.writer(wf)
            write.writerow(['mUrl'])


def save_to_csv(resultData):
    with open('./movieUrl.csv', 'a', newline='', encoding='utf-8') as wf:
        write = csv.writer(wf)
        write.writerow(resultData)

if __name__ == '__main__':
    init()
    main()